package com.cypher.encryptionanddecryption.fragment.protocol.kunlun;

import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.cypher.encryptionanddecryption.R;
import com.cypher.encryptionanddecryption.activity.BluetoothCommunicationActivity;
import com.cypher.encryptionanddecryption.bean.CommunicationBean;
import com.cypher.encryptionanddecryption.fragment.protocol.AbstractProtocolFragment;
import com.cypher.encryptionanddecryption.util.HexUtils;
import com.cypher.encryptionanddecryption.util.KunLunProtocolUtils;
import com.cypher.encryptionanddecryption.util.VariableUtils;

import org.apache.commons.lang3.StringUtils;

/**
 * 表号页面
 * @author: 古霸天
 * @date: 2020/12/25
 */
public class MeterNumberFragment extends AbstractProtocolFragment {
    // 远程关阀锁定
    private EditText meterNum;
    /**
     * 操作对象
     */
    private CommunicationBean bean;
    private BluetoothCommunicationActivity activity;
    public MeterNumberFragment(BluetoothCommunicationActivity activity, CommunicationBean bean) {
        this.activity = activity;
        this.bean = bean;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_kunlun_meter_number, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        meterNum = view.findViewById(R.id.meter_num);
        // 设置输入框输入整数
        meterNum.setInputType(InputType.TYPE_CLASS_NUMBER);
    }

    @Override
    public void fillFragment(String data) {
        meterNum.setText("");
        activity.getHandler().postDelayed(() -> {
            String formatData = KunLunProtocolUtils.formatReceiveData(data);
            meterNum.setText(StringUtils.substring(formatData, 6));
        }, 200);

    }
    @Override
    public String getHexCode(String rwType) {
        // 如果是只读的，hex码从实体中取
        if (StringUtils.equals("read", rwType))
            return KunLunProtocolUtils.getKunLunSpecialCommand(bean.getHexCode());
        // 如果是写数据，需要重新组包
        return  getWriteCode();
    }

    /**
     * 获取表号写入发送码
     * @return
     */
    private String getWriteCode() {
        String meterNumStr = meterNum.getText().toString().trim();
        if (StringUtils.isEmpty(meterNumStr)) {
            Toast.makeText(activity, "表号不能为空", Toast.LENGTH_SHORT).show();
            return VariableUtils.EMPTY;
        }
        // 表号必须为16位
        if (meterNumStr.length() != 16) {
            Toast.makeText(activity, "表号长度为16位", Toast.LENGTH_SHORT).show();
            return VariableUtils.EMPTY;
        }
        // 表号开头必须为133
        if (!StringUtils.equals("133", meterNumStr.substring(0, 3))) {
            Toast.makeText(activity, "表号不规范", Toast.LENGTH_SHORT).show();
            return VariableUtils.EMPTY;
        }

        String data = "00D6830008" + meterNumStr;
        data = HexUtils.paddingLeft(Integer.toHexString(data.length() / 2), 2) + data;

        return KunLunProtocolUtils.getKunLunCommand(KunLunProtocolUtils.conSelf + data);
    }
}
